package com.xm.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.logging.SimpleFormatter;

/**
 * @author XM, Email is huangwei_xm@163.com
 * @ClassName DemoController
 * @Package com.xm.demo.controller
 * @Description SpringMVC demo Controller
 * @Copyright: 训练营
 * @since 2021/4/8 16:28
 */
@Controller
@RequestMapping(value = "/demo")
public class DemoController {

    /*
    异常捕获统一处理，当前写入controller中，只能捕获当前controller产生的异常
    该异常捕获机制优先处理，捕获完成后，ExceptionResolver不进行处理
     */
    @ExceptionHandler(value = {Exception.class})
    public void exceptionHandler(Exception e, HttpServletResponse response) throws IOException {
        response.getWriter().print(e.getMessage());

    }


    @RequestMapping(value = "handler01")
    public ModelAndView handler01(HttpServletRequest request) {
        System.out.println("remote----------");
        System.out.println(request.getRemoteUser());
        System.out.println(request.getRemoteAddr());
        System.out.println(request.getRemoteHost());
        System.out.println(request.getRemotePort());
        System.out.println(getMACAddress(request.getRemoteAddr()));
        System.out.println("local----------");
        System.out.println(request.getLocalAddr());
        System.out.println(request.getLocalName());
        System.out.println(request.getLocalPort());

        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        System.out.println("class=DemoController,method=handler01");
        return modelAndView;
    }

    public String getMACAddress(String ip) {
        String str = "";
        String macAddress = "";
        try {
            Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
            InputStreamReader ir = new InputStreamReader(p.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (int i = 1; i < 100; i++) {
                str = input.readLine();
                if (str != null) {
                    if (str.indexOf("MAC Address") > 1) {
                        macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return macAddress;
    }

    @RequestMapping(value = "upload", method = {RequestMethod.POST})
    public ModelAndView upload(MultipartFile file, HttpSession httpSession) throws IOException {
        String contextPath = httpSession.getServletContext().getContextPath();
        String originalFilename = file.getOriginalFilename();
        String subffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String targetDir = contextPath + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        File targetDirFile = new File(targetDir);
        if (!targetDirFile.exists()) {
            targetDirFile.mkdirs();
        }
        String targetFilePath = targetDir + "/" + UUID.randomUUID().toString() + subffix;
        System.out.println("class=DemoController,method=upload,targetFilePath=" + targetFilePath);
        file.transferTo(new File(targetFilePath));
        System.out.println("class=DemoController,method=upload,fileName=" + file.getOriginalFilename());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("date", "上传成功");
        return modelAndView;
    }

    // 转发和重定向

}
